import React from 'react'
import { useRoutes,BrowserRouter } from 'react-router-dom'
import routes from '~react-pages'
import './App.css'
function App() {
  const Routes = ()=>{
    React.navigate = useNavigate()
    const {pathname} = useLocation()
    if(!React.RouterRules.includes(pathname))check()
    return useRoutes(routes)
  }
  const check = async()=>{
    if(React.business.id)return true
    const token = React.cookies.load('token')
    if(!token){
      React.toast('未登录','/business/login','fail')
      return false
    }
    let result = await React.request.get(`/business/check?token=${token}`)
    if(result.code==1){
      React.business = result.data
      return true
    }else{
      React.cookies.remove('token');
      React.toast('未登录','/business/login','fail')
      return false
    }
  }
  //页面加载中loading...
  const fallback = () => (<React.UI.AutoCenter><React.UI.DotLoading color='primary' /></React.UI.AutoCenter>)
  return (
    <>
      <BrowserRouter future={{v7_startTransition: true, v7_relativeSplatPath: true}}>
      <React.Suspense fallback={fallback()}>
        <Routes />
      </React.Suspense>
    </BrowserRouter>
    </>
  )
}

export default App
